//
//  YFWelcomeView.swift
//  仿新浪微博
//
//  Created by 杨帆 on 16/7/5.
//  Copyright © 2016年 杨帆. All rights reserved.
//

import UIKit
import SDWebImage

class YFWelcomeView: UIView {

    @IBOutlet weak var iconVew: UIImageView!
    
    @IBOutlet weak var tipLabel: UILabel!
    
    @IBOutlet weak var bottomCons: NSLayoutConstraint!
    
    // 图标宽度约束
    @IBOutlet weak var iconWidthCons: NSLayoutConstraint!
    
    class func welcomeView() ->YFWelcomeView {
    
        let nib = UINib(nibName: "YFWelcomeView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! YFWelcomeView
        
        // 从xib 加载视图 默认大小是600x600
        v.frame = UIScreen.main().bounds
        
        return v 
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        print("initWithCoder + \(iconVew)")
        
    }
    
    override func awakeFromNib() {
        
        // 1. url
        guard let urlString = YFNetWorkManager.shared.userAccount.avatar_large,
        
        let url = URL(string: urlString) else {
        
            return
        }
        
        // 2. 设置头像- 设置占位图像
        iconVew.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
        
        // 3. 设置圆角
        iconVew.layer.cornerRadius = iconWidthCons.constant * 0.5
        iconVew.layer.masksToBounds = true
        
    }
    // 视图被添加到window 上 表示视图已经显示
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        self.layoutIfNeeded()
        
        bottomCons.constant = bounds.size.height - 200
        
        // 如果控件的Frame还没有计算好 所有的约束会一起动画
        UIView.animate(withDuration: 1.0,
        
            delay: 0,
            usingSpringWithDamping: 0.7,
            initialSpringVelocity: 0,
            options: [],
            animations: {
                
                // 更新约束
                self.layoutIfNeeded()
        }) { (_) in
        
            UIView.animate(withDuration: 1.0, animations: { 
                
                self.tipLabel.alpha = 1
                }, completion: { (_) in
                    
                    self.removeFromSuperview()
            })
        }
    }
}
